package personal.wang.handler;

import com.google.protobuf.InvalidProtocolBufferException;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import personal.wang.abs.AbstractMessageHandler;
import personal.wang.domain.TestpointInfoProto;
import personal.wang.job.TestPointV2;
import personal.wang.manager.RecordGeneratorManager;
import personal.wang.message.ACKMessage;
import personal.wang.message.Message;

/**
 * @className: WorkerTemplateHandler
 * @Description:
 * @Author: wangyifei
 * @Date: 2025/2/19 16:33
 */
public class WorkerTemplateHandler extends AbstractMessageHandler {
    private static Logger logger = LoggerFactory.getLogger(WorkerTemplateHandler.class);
    private RecordGeneratorManager recordGeneratorManager;

    public WorkerTemplateHandler(RecordGeneratorManager recordGeneratorManager) {
        this.recordGeneratorManager = recordGeneratorManager ;
    }

    @Override
    public void handler(Message message, ChannelHandlerContext ctx) {

        logger.info("receiver {}" , message.getHeader());
        TestpointInfoProto.TestPointInfo tt = null ;
        try {
            tt = TestpointInfoProto.TestPointInfo.parseFrom(message.getContent());
        } catch (InvalidProtocolBufferException e) {
            throw new RuntimeException(e);
        }
        TestPointV2 testPointV2 = new TestPointV2();
        testPointV2.setTestPointInfo(tt);
        testPointV2.execute();
        // todo: 当执行完任务之后，发送 ack
//        ctx.writeAndFlush(new
//                ACKMessage());
        super.handler(message, ctx);
    }

    public void setRecordGeneratorManager(RecordGeneratorManager recordGeneratorManager) {
        this.recordGeneratorManager = recordGeneratorManager;
    }

    public RecordGeneratorManager getRecordGeneratorManager() {
        return recordGeneratorManager;
    }
}
